<template>
  <div class="container">
    <div class="search-box">
      <!-- 只要是收集数据的地方都可以用双向数据绑定 -->
      <el-input placeholder="请输入内容" class="input-box" v-model="text"></el-input>
      <el-button @click="clear">清除</el-button>
      <el-button type="primary" @click="searchTask">搜索</el-button>
    </div>
    <el-table border style="width: 100%" :data="dataList">
      <el-table-column type="index" label="任务编号" width="80"> </el-table-column>
      <el-table-column prop="name" label="任务名称"> </el-table-column>
      <el-table-column prop="des" label="任务描述"> </el-table-column>
      <el-table-column fixed="right" label="操作" width="180">
        <template #default="{ row }">
          <el-button size="mini" style="margin-right: 10px" @click="editTask(row.id)"
            >编辑</el-button
          >
          <el-popconfirm
            confirmButtonText="好的"
            cancelButtonText="不用了"
            icon="el-icon-info"
            iconColor="red"
            title="确定删除任务吗？"
            @confirm="getConfim(row.id)"
          >
            <template #reference>
              <el-button size="mini" type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div class="pageContainer">
      <el-pagination
        layout="prev, pager, next"
        :total="total"
        :page-size="params._limit"
        @current-change="changePage"
      >
      </el-pagination>
      <el-button size="small" plain @click="addTask">+ 新增</el-button>
    </div>
    <!-- 弹框 -->
    <el-dialog
      :title="form.id ? '编辑任务' : '新增任务'"
      :visible="showDialog"
      width="30%"
      @close="closeDialog"
    >
      <template #default>
        <el-form ref="form" :model="form" label-width="80px" :rules="rules">
          <el-form-item label="任务名称" prop="name">
            <el-input v-model="form.name"></el-input>
          </el-form-item>
          <el-form-item label="任务描述" prop="des">
            <el-input v-model="form.des"></el-input>
          </el-form-item>
        </el-form>
      </template>
      <template #footer>
        <el-button @click="closeDialog">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import {
  getTaskListAPI,
  addTaskAPI,
  editTaskDetailAPI,
  editTaskAPI,
  delTaskAPI,
  searchTaskAPI
} from '@/api/task.js'
import { v4 as uuidv4 } from 'uuid'
export default {
  name: 'App',
  data() {
    return {
      params: {
        _page: 1, // 当前第几页
        _limit: 5 // 每页条数
      },
      // 渲染数据
      dataList: [],
      total: 0,
      showDialog: false,
      form: {
        name: '',
        des: ''
        // id: null
      },
      rules: {
        name: [{ required: true, message: '任务名称必填', trigger: 'blur' }],
        des: [{ required: true, message: '任务描述必填', trigger: 'blur' }]
      },
      // 搜索表单的值
      text: ''
    }
  },
  mounted() {
    this.getTaskList()
  },
  methods: {
    // 获取列表
    async getTaskList() {
      const res = await getTaskListAPI(this.params)
      this.dataList = res.data
      this.total = Number(res.headers['x-total-count'])
      // console.log(res)
      // console.log(this.total)
    },
    // 分页
    changePage(page) {
      // 将得到的页数给_page(发送给服务端的数据)
      this.params._page = page
      // 然后获取当前页最新的数据
      this.getTaskList()
    },
    // 点击新增，弹框显示
    addTask() {
      this.showDialog = true
    }, // 关闭弹框
    closeDialog() {
      // 关闭弹框
      this.showDialog = false
      // 清空表单+校验
      this.$refs.form.resetFields()
      this.form = {
        name: '',
        des: ''
      }
    },
    // 提交表单
    confirm() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          if (this.form.id) {
            // 编辑任务
            await editTaskAPI(this.form)
          } else {
            // 添加任务
            await addTaskAPI({ ...this.form, id: Number(uuidv4()) })
          }
          // 提示用户
          this.$message.success(`${this.form.id ? '编辑成功' : '新增成功'}`)
          // 更新列表
          this.getTaskList()
          // 关闭弹框+清空表单+清空校验
          this.closeDialog()
        }
      })
    },
    // 查看任务详情
    async editTask(id) {
      this.showDialog = true
      const res = await editTaskDetailAPI(id)
      // 回显数据
      this.form = res.data
    },
    // 删除任务
    async getConfim(id) {
      await delTaskAPI(id)
      // 更新列表
      this.getTaskList()
    },
    // 搜索任务
    async searchTask() {
      // 点击搜索，发送搜索任务的请求
      const res = await searchTaskAPI(this.text)
      // 然后将获得数据赋值给表格的数据
      this.dataList = res.data
    },
    // 点击清空，清除搜索的input框内容
    clear() {
      this.text = ''
      this.getTaskList()
    }
  }
}
</script>

<style scoped lang="less">
.container {
  width: 800px;
  margin: 0 auto;
}
.search-box {
  display: flex;
  margin-bottom: 10px;
  .input-box {
    margin-right: 20px;
  }
}
.pageContainer {
  margin-top: 10px;
  display: flex;
  justify-content: space-between;
}
</style>
